/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import LogUtils from '../utils/LogUtils';

import Constants from './Constants'

const TAG = 'CustomPSDViewModel'

export default class CustomPSDViewModel {
  prompt: string = '输入';
  passwordArr: string[] = ['', '', '', '', '', ''];
  numKeyboard: any[] = Constants.NUMKEY_BOARD;

  constructor() {
    // super();
  }

  ViewModelInit(): void {
    LogUtils.d(TAG, 'ViewModelInit');
    // super.ViewModelInit();
  }

  getFreezingTimeNm(freezingMillisecond: number): string {
    const MINUTE_NM = '分钟'
    const SECOND_NM = "秒"
    let minute = Math.floor(freezingMillisecond / (60 * 1000));
    let second = Math.round((freezingMillisecond % (60 * 1000)) / 1000);
    let timeName = '';
    if (minute != 0) {
      timeName += minute + MINUTE_NM
    }
    if (second != 0) {
      timeName += second + SECOND_NM
    }
    return timeName;
  }

  countdown(freezingTime) {
    const TRY_AGAIN = "后重试"
    let promptText: string = '';
    let freezingMillisecond = freezingTime;
    LogUtils.d(TAG, 'countdown freezingTime: ' + freezingTime)
    if (freezingMillisecond > 0) {
      promptText = this.getFreezingTimeNm(freezingMillisecond);
      promptText += TRY_AGAIN;
      setTimeout(this.countdown.bind(this), Constants.INTERVAL, freezingTime - 1000)
    } else {
      promptText = '请输入密码'
    }
    this.prompt = promptText;
  }

  onKeyPress(index, callback) {
    LogUtils.d(TAG, `onKeyPress start param: ${index}`);
    let keyValue = this.numKeyboard[index].value;
    LogUtils.d(TAG, `onKeyPress start param: ${keyValue}`);

    if (keyValue > 0 || keyValue === 0) {
      const index = this.passwordArr.map(item => item).indexOf('');
      LogUtils.d(TAG, index.toString(), 'index');
      if (index > -1) {

        this.passwordArr[index] = keyValue;
        this.numKeyboard[11].row1 = '删除';
        this.numKeyboard[11].value = Constants.DEL_PWD;

        this.updateStorage(callback);
      }
    } else if (keyValue == Constants.DEL_PWD) {
      const index = this.passwordArr.map(item => item).indexOf('')
      LogUtils.d(TAG, 'onKeyPress index: ', index)
      if (index === -1) {
        this.passwordArr[5] = ''
      } else if (index === 1) {
        this.passwordArr[index-1] = ''
        this.numKeyboard[11].row1 = '返回';
        this.numKeyboard[11].value = Constants.GO_BACK;
      } else {
        this.passwordArr[index-1] = ''
      }
      this.updateStorage(callback);
    } else if (keyValue == Constants.GO_BACK) {
      // router.back()
      // service.goBack();
    } else if (keyValue == Constants.CALL_PHONE) {
      AppStorage.SetOrCreate('cueWord', '请在60秒后重试');
      this.updateStorage(callback);
    }
  }

  onCallPhone() {
    LogUtils.i(TAG, 'onCallPhone');
  }

  onAuthPassword(callback: unknown) {
    LogUtils.i(TAG, `onAuthPassword`);
    if (this.passwordArr.length == 0) {
      this.updateStorage(callback);
      return;
    }
    // service.authUser(AuthSubType.PIN_MIXED, this.passwordArr, (result, extraInfo) => {
    //     this.clearPassword();
    //     if (result == 0) {
    //         //unlock the screen
    //         service.unlocking();
    //     } else {
    //         //Clear the entered password
    //         super.changePrompt(extraInfo.remainTimes, extraInfo.freezingTime, callback);
    //     }
    // });
  }

  clearPassword() {
    LogUtils.i(TAG, `clearPassword`)
    this.passwordArr = [];
    this.numKeyboard[11].value = Constants.GO_BACK;
    this.updateStorage(() => {
    })
  }

  updateStorage(callback) {
    LogUtils.i(TAG, `updateStorage child`)
    // refresh  the page
    AppStorage.SetOrCreate('passwordArr', this.passwordArr);
    AppStorage.SetOrCreate('numKeyboard', this.numKeyboard);
    // super.updateStorage(callback)
    callback()
  }
}
